﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MetadataBasedConfigArch.Framework.Configurable;
using MetadataBasedConfigArch.Framework.Model;
using MetadataBasedConfigArch.Framework;
using MetadataBasedConfigArch.Framework.Processor;
using System.Data.Objects.DataClasses;

namespace MetadataBasedConfigArch.Model
{
    public abstract class TableConfigurableEntityObject : System.Data.Objects.DataClasses.EntityObject
    { 
        #region ITableConfigurable Members

        public IEnumerable<KeyValuePair<String, Object>> ExtendProperties
        {
            get;
            set;
        }

        public IEnumerable<IConfigMetaConfigData> ConfigDatasInst
        {
            get;
            set;
        }

        public IConfigMeta ConfigMetaInst
        {
            get;
            set;
        }
        
        public string ConfigMetaContent
        {
            get { return GetTableName(); }
        }


        public long DataID
        {
            get { return GetDataID(); }
        }

        #endregion

        #region Virtual Methods

        protected virtual Int64 GetDataID()
        { 
            if (ConfigMetaInst != null)
            {
                var dataIdPropertyName = ConfigMetaInst.DataKey;
                if (!String.IsNullOrEmpty(dataIdPropertyName))
                {
                    var propertyValue = this.GetPropertyValue(dataIdPropertyName);
                    if (propertyValue.GetType() == typeof(Int64))
                    {
                        return Convert.ToInt64(this.GetPropertyValue(dataIdPropertyName));
                    }
                }
            }
            return 0;
        }
        
        public virtual string GetTableName()
        {
            object[] objs = this.GetType().GetCustomAttributes(typeof(EdmEntityTypeAttribute), true);
            if (objs != null && objs.Length > 0)
            {
                EdmEntityTypeAttribute entityTypeAttr = objs[0] as EdmEntityTypeAttribute;
                if (entityTypeAttr != null)
                {
                    return entityTypeAttr.Name;
                }
            }
            return this.GetType().Name;

        }

        #endregion

        protected IConfigurableProcessor ConfigurableProcessor
        {
            get;
            set;
        }

        public void LoadConfigurableInfos(IMetadataBasedEntityProvider entityProvider)
        { 
            if (this is IConfigurable)
            {
                ConfigurableProcessor = ConfigurableProcessorFactory.Create(this as IConfigurable, entityProvider);
                if (ConfigurableProcessor != null)
                {
                    ConfigurableProcessor.LoadConfigurableInfos();
                }
            }
        }

        public void SetConfigurableInfo(String configContent, Object value)
        {
            if (ConfigurableProcessor != null)
            {
                ConfigurableProcessor.SetConfigurableInfo(configContent, value);
            }
        }

        public Object GetConfigurableInfo(String configContent)
        {
            if (ConfigurableProcessor != null)
            {
                return ConfigurableProcessor.GetConfigurableInfo(configContent);
            }
            return null;
        }

    }
}
